<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0,1)">添加权限</el-button>
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" size="mini" type="text" @click="addPermission(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">修改</el-button>
            <el-popconfirm title="确定删除吗？" @onConfirm="delPermission(row.id)">
              <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 展示添加修改弹框 -->
    <el-dialog v-dialogDrag width="500px" :title="showText" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="formData" :model="formData" label-width="120px" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" type="textarea" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model=" formData.enVisible" size="mini" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="btnOK">确定</el-button>
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, updatePermission, getPermissionDetail } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [], // 权限数据列表
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '', // 类型该类型不需要显示，因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到那个节点下
        enVisible: '0'// 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称是必填项', trigger: 'blur' }],
        code: [{ required: true, message: '权限代码是必填项', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0)
      // console.log(this.list)
    },
    // 添加权限按钮
    addPermission(pid, type) {
      // console.log(pid, type)
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 新增确认按钮
    btnOK() {
      this.$refs.formData.validate(async isOk => {
        if (isOk) {
          if (this.formData.id) {
            await updatePermission(this.formData)
            this.$message.success('更新数据成功')
          } else {
            await addPermission(this.formData)
            this.$message.success('添加成功')
          }

          this.getPermissionList()
          this.btnCancel()
        }
      })
    },
    // 弹框取消按钮
    btnCancel() {
      this.$refs.formData.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '', // 类型该类型不需要显示，因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到那个节点下
        enVisible: '0'// 默认关闭
      }
      this.showDialog = false
    },
    // 获取更新的model
    async editPermission(id) {
      const data = await getPermissionDetail(id)
      // console.log(data)
      this.formData = data
      this.showDialog = true
    },
    // 删除权限
    async delPermission(id) {
      await delPermission(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    }
  }
}
</script>
<style scoped lang="scss">
.btn-add{
  margin: 10px;
}
.el-form-item{
  .el-input,.el-textarea{
    width:90%
  }
}
</style>
